package com.tianqingwl.gsystem.resources.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianqingwl.gsystem.resources.mapper.SysConfigMapper;
import com.tianqingwl.gsystem.resources.pojo.SysConfig;
import com.tianqingwl.gsystem.resources.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysConfigServiceImpl implements SysConfigService {

    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Override
    public List<String> listAllConfigGroup() {
        List<String> configGroupList = sysConfigMapper.listAllConfigGroup();
        return configGroupList;
    }

    @Override
    public void list(Page<SysConfig> sysConfigPage, Integer searchType, String searchStr, String configGroup) {
        QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<>();
        //搜索类型对搜索参数封装
        if (searchType != null && !StringUtils.isEmpty(searchStr)) {
            switch (searchType) {
                case 1:
                    queryWrapper.like("configKey", searchStr);
                    break;
                case 2:
                    queryWrapper.like("configValue", searchStr);
                    break;
                case 3:
                    queryWrapper.like("configDesc", searchStr);
                    break;
            }
        }
        if (!StringUtils.isEmpty(configGroup)) {
            queryWrapper.eq("configGroup", configGroup);
        }
        queryWrapper.orderByDesc("configId");
        sysConfigMapper.selectPage(sysConfigPage, queryWrapper);
    }

    @Override
    public SysConfig getSysConfig(Long configId) {
        SysConfig sysConfig = sysConfigMapper.selectById(configId);
        return sysConfig;
    }

    @Override
    public void save(SysConfig sysConfig) {
        if (sysConfig != null) {
            if (sysConfig.getConfigId() != null) {
                //编辑
                sysConfigMapper.updateById(sysConfig);
            } else {
                //新增
                sysConfigMapper.insert(sysConfig);
            }
        }
    }

    @Override
    public SysConfig getSysConfig(String configGroup, String configKey) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("configGroup", configGroup);
        queryWrapper.eq("configKey", configKey);
        SysConfig sysConfig = sysConfigMapper.selectOne(queryWrapper);
        return sysConfig;
    }

    @Override
    public List<SysConfig> getSysConfigByKey(String configKey) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("configKey", configKey);
        List<SysConfig> configList = sysConfigMapper.selectList(queryWrapper);
        return configList;
    }

    @Override
    public Map getMapByKey(String key) {
        return null;
    }

    @Override
    public List<SysConfig> getSysConfigByGroup(String configGroup) {
        QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("configGroup", configGroup);

        List<SysConfig> configList = sysConfigMapper.selectList(queryWrapper);
        return configList;
    }

    @Override
    public Map getMapByGroup(String configGroup) {
        List<SysConfig> configList = getSysConfigByGroup(configGroup);
        //转map
        Map map = new HashMap();
        for (SysConfig sysConfig : configList) {
            if(map.get(sysConfig.getConfigKey())==null){
                map.put(sysConfig.getConfigKey(), sysConfig.getConfigValue());
            }else{
                //TODO 有漏洞，考虑同group下key不能重复
                //该键存在则集合组装(组装这个术语能提升逼格，要多写)
                Object o = map.get(sysConfig.getConfigKey());
                if (o instanceof List){
                    List list = (List)o;
                    Map map1= new HashMap();
                    map1.put(sysConfig.getConfigKey(), sysConfig.getConfigValue());
                    list.add(map1);
                    map.put("list",list);
                }else{
                    List list = new ArrayList();
                    //旧数据
                    list.add(o);
                    Map map1= new HashMap();
                    map1.put(sysConfig.getConfigKey(), sysConfig.getConfigValue());
                    list.add(map1);
                    map.put("list",list);
                }
            }

        }
        return map;
    }
}
